/**
 * @param {Array} position = [114.30, 40.50]
 * @param {Array} attr = [{name:"name",type:"string",value:"Point"}]
 * @param {Object} service = {name:"xzd-map",layerId:2}
 *  */

class Point {
  static add({ position, attr = [], service, docLayer }) {
    var gpoint = new Zondy.Object.GPoint(position[0], position[1]) //createPoint();
    //设置当前点要素的几何信息
    var fGeom = new Zondy.Object.FeatureGeometry({
      PntGeom: [gpoint]
    })
    /* 1-2、style */
    var pointInfo = new Zondy.Object.CPointInfo({
      Angle: 0,
      Color: 11, //子图颜色
      Space: 0,
      SymHeight: 6,
      SymID: 31, //子图号
      SymWidth: 6
    })
    //设置当前点要素的图形参数信息
    var webGraphicInfo = new Zondy.Object.WebGraphicsInfo({
      /* 1-Point  2-LineString 3-Area */
      InfoType: 1,
      PntInfo: pointInfo
    })
    /* 1-3、属性 */
    //设置添加点要素的属性信息
    var attValue = attr.map((item) => item.value)
    var feature = new Zondy.Object.Feature({
      fGeom: fGeom,
      GraphicInfo: webGraphicInfo,
      AttValue: attValue
    })
    //设置要素为点要素
    feature.setFType(1)
    /* 2、将创建的mapgis的要素集添加要素 source */
    /* new ol.source.Vector() */
    var featureSet = new Zondy.Object.FeatureSet()
    //设置属性结构  和mapgis中的图层映射
    var cAttStruct = new Zondy.Object.CAttStruct({
      FldName: attr.map((item) => item.name),
      FldNumber: attr.length,
      FldType: attr.map((item) => item.type)
    })
    featureSet.AttStruct = cAttStruct
    //添加要素到要素数据集
    featureSet.addFeature(feature)
    /* 3、调用服务执行添加 */
    var editService = new Zondy.Service.EditDocFeature(service.name, service.layerId, {})
    //执行添加点要素功能
    editService.add(featureSet, this.onPntSuccess(docLayer))
  }
  static update({ position = [], attr, service, docLayer, color,id }) {
    let fGeom = null
    if (position.length) {
      var gpoint = new Zondy.Object.GPoint(position[0], position[1])
      fGeom = new Zondy.Object.FeatureGeometry({ PntGeom: [gpoint] })
    }
    /* 样式信息 */
    var pointInfo = new Zondy.Object.CPointInfo({
      Angle: 0,
      Color: color, //子图颜色
      Space: 0,
      SymHeight: 5,
      SymID: 21, //子图号
      SymWidth: 5
    })
    //设置当前点要素的图形参数信息
    var webGraphicInfo = new Zondy.Object.WebGraphicsInfo({
      /* 1-Point  2-LineString 3-Area */
      InfoType: 1,
      PntInfo: pointInfo
    })
    //设置添加点要素的属性信息
    var attValue = attr.map((item) => item.value)
    //创建一个点要素
    /* 修改时候属性信息不能省略 */
    /* 几何,属性信息 */
    var newFeature = new Zondy.Object.Feature({
      fGeom: fGeom,
      GraphicInfo: webGraphicInfo,
      AttValue: attValue
    })
    //设置要素为点要素
    newFeature.setFType(1)
    newFeature.setFID(1) //设置修改要素的fid值
    //创建一个点要素数据集
    var featureSet = new Zondy.Object.FeatureSet()
    featureSet.clear()
    //设置属性结构
    var cAttStruct = new Zondy.Object.CAttStruct({
      FldName: attr.map((item) => item.name),
      FldNumber: attr.length,
      FldType: attr.map((item) => item.type)
    })
    featureSet.AttStruct = cAttStruct
    //添加要素到要素数据集
    featureSet.addFeature(newFeature)
    /* 调用服务 修改 */
    //创建一个编辑服务类
    var editService = new Zondy.Service.EditDocFeature(service.name, service.layerId, {})
    editService.deletes(id)
    // editService.update(featureSet, this.onPntSuccess(docLayer))
    editService.add(featureSet, this.onPntSuccess(docLayer))
  }
  static onPntSuccess(docLayer) {
    return function (data) {
      if (data.succeed) {
        alert('修改事件成功！')
        docLayer.refresh()
      } else {
        alert('修改事件失败！')
      }
    }
  }
  static onChangeSuccess(docLayer) {
    return function (data) {
      if (data.succeed) {
        docLayer.refresh()
      } else {
      }
    }
  }
}
export { Point }